from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q
from app.controllers.marketing.deposit import deposit_controller
from app.models import CustomerUser
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.marketing import DepositUpdate, DepositCreate

deposit_router = APIRouter()
model_name = "储值"


@deposit_router.get("/list_add", summary=f"查看{model_name}列表")
async def do_list_add(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        user_name: str = Query(None, description=f"用户姓名"),
        order_id: int = Query(None, description=f"单号"),
):
    search_user_ids = []
    if user_name:
        search_users = await CustomerUser.filter(name__contains=user_name).values("id")
        search_user_ids = {orders['id'] for orders in search_users}  # 获取所有的 user_id
    q = Q(direction=1)
    if user_name:
        q &= Q(user_id__in=search_user_ids)
    if order_id:
        q &= Q(order_id=order_id)
    total, data = await deposit_controller.do_list(page=page, page_size=page_size, search=q)

    user_ids = {orders['user_id'] for orders in data}  # 获取所有的 user_id
    users = await CustomerUser.filter(id__in=user_ids)
    users_dict = {user.id: user.name for user in users}
    for item in data:
        item['user_name'] = users_dict.get(item['user_id'], "已删除用户")
        if item['direction'] == 1:
            item['direction_name'] = "增加"
        else:
            item['direction_name'] = "减少"

    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@deposit_router.get("/list_del", summary=f"查看{model_name}列表")
async def do_list_del(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        user_name: str = Query(None, description=f"用户姓名"),
        order_id: int = Query(None, description=f"单号"),
):
    search_user_ids = []
    if user_name:
        search_users = await CustomerUser.filter(name__contains=user_name).values("id")
        search_user_ids = {orders['id'] for orders in search_users}  # 获取所有的 user_id
    q = Q(direction=2)
    if user_name:
        q &= Q(user_id__in=search_user_ids)
    if order_id:
        q &= Q(order_id=order_id)
    total, data = await deposit_controller.do_list(page=page, page_size=page_size, search=q)

    user_ids = {orders['user_id'] for orders in data}  # 获取所有的 user_id
    users = await CustomerUser.filter(id__in=user_ids)
    users_dict = {user.id: user.name for user in users}
    for item in data:
        item['user_name'] = users_dict.get(item['user_id'], "已删除用户")
        if item['direction'] == 1:
            item['direction_name'] = "增加"
        else:
            item['direction_name'] = "减少"

    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@deposit_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await deposit_controller.do_detail(data_id=data_id)
    return Success(data=data)

@deposit_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: DepositCreate,
):
    await deposit_controller.do_create(obj_in=category_in)
    return Success(msg="创建成功")

@deposit_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: DepositUpdate,
):
    await deposit_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@deposit_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await deposit_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")
